package cm.emp.servlet;


import cm.emp.bean.Person;
import cm.emp.dao.UserLoginDao;
import cm.emp.service.UserLoginService;
import cm.emp.service.UserLoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/userLogin")
/**
 * Description:
 * Date: 2024/5/26
 * User: 21719
 */
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首先拿到前端传过来的信息
        String userAccount = req.getParameter("userAccount");
        String userPassword = req.getParameter("userPassword");
        Person person = new Person(userAccount,userPassword);
        UserLoginService ul=new UserLoginServiceImpl();
        Person newPerson=ul.getLogin(person);
        if(newPerson!=null){
            //进主页面
            //req.getSession().setAttribute("session-person",newPerson);
            HttpSession session=req.getSession();
            session.setAttribute("session_person",newPerson);
            req.setAttribute("mainRight","blank.jsp");
            req.getRequestDispatcher("main.jsp").forward(req,resp);
        }else{
            //继续回到登录页面
            req.setAttribute("tip","账户密码不一致");
            req.getRequestDispatcher("userLogin.jsp").forward(req,resp);
        }
    }
}
